<template>
	<view class="bgw">
		<view class="item text-center font-bold">
			{{title}}
		</view>
		<view class="item bg-gray">
			<view class="flex font-24 text-light-gray">
				<view class="text-center" style="width: 40%;">
					部门
				</view>
				<view class="text-center flex-1 ml15">
					份数 （ {{sales_qty}} )
				</view>
			</view>
		</view>
		<scroll-view scroll-y scroll-with-animation class="" style="max-height: 300px;">
			<template v-for="(v,i) in list">
				<view class="flex item">
					<view class="text-center" style="width: 40%;">
						{{v.name}}
					</view>
					<view class="text-center flex-1" v-if="!readonly">
							<input v-model="qty[i]" type="number" placeholder="请输入" class="input_border_bottom"/>
					</view>
					<view class="text-center flex-1" v-if="readonly">
						{{qty[i]}}
					</view>
				</view>
				<u-line dashed></u-line>
			</template>
		</scroll-view>
		<view class="blank20"></view>
		<view class="wrapper">
			<view class="flex">
				<view class="flex-1" v-if='readonly'>
					<u-button type="info" text="关闭" size="small" @click="cancel"></u-button>
				</view>
				<view class="flex-1 mr10" v-if='!readonly'>
					<u-button type="info" text="取消修改" size="small" @click="cancel"></u-button>
				</view>
				<view class="flex-1 ml10" v-if='!readonly'>
					<u-button type="primary" text="保存修改" size="small" @click="confirm"></u-button>
				</view>
			</view>
		</view>
		<view class="blank20"></view>
	</view>
</template>

<script>
	export default {
		name: "division",
		props: {
			title:String,
			list: Array,
			readonly:Boolean,
		},
		data() {
			return {
				qty: [],
			};
		},
		watch: {

		},
		created(){
			this.qty = [];
			this.list.forEach((v,i)=>{
				this.qty.push(v.sales_qty);
			})
		},
		filters: {
			
		},
		computed: {
			sales_qty(){
				let num = 0;
				this.qty.forEach(v=>{
					num+= Number(v);
				});
				return num
			},
		},
		methods: {
			value_blur(){
				
				
			},
			save(){

			},
			confirm(){
				let division_qty_json = {};
				let flag = false;
				this.list.forEach((v,i)=>{
					let sales_qty = Number(this.qty[i]);
					if(sales_qty != v.sales_qty){
						flag = true;
					}
					division_qty_json[v.id] = sales_qty;
				})
				let data = {
					sales_qty:this.sales_qty,
					division_qty_json:division_qty_json
				};
				if(flag){
					this.$emit("confirm",data);
				}else{
					this.$emit("cancel");
				}
			},
			cancel(){
				this.$emit("cancel");
			}
		}

	}
</script>

<style>

</style>